<template>
  <yt-page title="新增执法企业">
    <yt-cell label="单位名称">
      <yt-cell-select v-model="form.plan_enterprise" :list="entList" label="EnterpriseName" val="id"></yt-cell-select>
    </yt-cell>
    <yt-cell label="检查时间">
      <yt-cell-time v-model="form.plan_time" format="YYYY-MM"></yt-cell-time>
    </yt-cell>
    <yt-cell label="企业类别">
      <yt-cell-select v-model="form.enterprise_category" :list="categoryList" label="D_text" val="D_code"></yt-cell-select>
    </yt-cell>
    <yt-cell label="类别简称">
      <yt-cell-select v-model="form.category_short" :list="shortList" label="D_text" val="D_code"></yt-cell-select>
    </yt-cell>
    <yt-btn :loading="submit" class="m-t">提交</yt-btn>
  </yt-page>
</template>

<script>
import { BuildPointer } from '@/common/utils/index.js'
import { commonApi } from '@/common/api/commonApi.js'

export default {
  data() {
    return {
      form: {},
      entList: [],
      categoryList: [],
      shortList: []
    }
  },
  activated() {
    this.fetchData()
  },
  methods: {
    async fetchData() {
      let p = [
        commonApi.search('ent_enterprise'),
        commonApi.dictionaries(['enterprise_category', 'category_short'])
      ]
      let [ent, [categroy, short]] = await Promise.all(p)
      this.entList = ent
      this.categoryList = categroy
      this.shortList = short
    },
    async submit(loaded) {
      try {
        let form = BuildPointer(this.form, {
          plan_enterprise: 'ent_enterprise'
        })
        await commonApi.save('law_plan', { ...form, state: 0 })
        this.$router.$back()
        this.$Toast('新增成功!', 'success')
      } catch (e) {
        this.$Toast('新增失败!', 'error')
      }
      loaded()
    }
  }
}
</script>
